package com.pxxy.springboot.webcase.controller;

import com.pxxy.springboot.webcase.entity.User;
import com.pxxy.springboot.webcase.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

/******************
 * @Author yejf
 * @Description TODO
 */
@Controller
@RequestMapping("/user")
public class UserController {

    private static final Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @RequestMapping("/toLogin")
    public String toLogin() {
        //直接返回视图
        return "user/login"; //  真正的视图名：templates/user/login.html
    }

    @RequestMapping("/login")
    public String login(@RequestParam("name") String username,
                        @RequestParam("pwd") String pwd,
                        @RequestParam("checkcode") String checkCode, Model model, HttpSession session) {
        //打印出来
        log.debug("登录请求的三个参数值分别是："+username+","+pwd+","+checkCode);
        //调用业务方法
        final User user = this.userService.login(username, null);
        //1. 判断
        if(user == null) {
            //说明此用户不存在
            model.addAttribute("USER_NOT_EXIST", "用户不存在");
            //返回
            return "user/login";
        } else {
            //2. 继续比较密码
            if(user.getPassword().equals(pwd)) {
                //说明密码相同
                log.debug("登录成功...");
                //把当前用户绑定到 session 范围
                session.setAttribute("LOGIN_USER", user);
                //跳 转 到 bookcontroller中的 list方法
                return "redirect:/book/list.do";
            } else {
                //说明密码不相同
                model.addAttribute("PWD_ERROR",  "密码不正确.");
                //返回 [以forward: 开头，则不经过视图解析器]
                return "user/login";
            }
        }
    }

    //
    @RequestMapping("/toRegister")
    public String toRegister() {
        //
        return "user/register";
    }

    @RequestMapping("/register")
    public String register(User user, String pwd2, Model model) {
        //
        log.debug("用户注册时提交的用户名和密码是："+user.getUserName()+", "+user.getPassword()+" ,二次密码："+pwd2);
        //判断
        if(user.getPassword().equals(pwd2)) {
            // 调用业务方法
            this.userService.register(user);
            log.debug("注册成功");
            //model.addAttribute("register_status",true);
            //跳到登录页面
            return "user/login";

        } else {
            //二次密码不一样
            model.addAttribute("CHECK_PWD_ERROR", "二次输入的密码不一样");
            //
            return "user/register";
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        //销毁session
        log.debug("用户退出，销毁当前客户的会话");
        session.invalidate();
        //
        return "user/login";
    }
}
